//
// codebook.h
//
// author: vietcuong
//

#ifndef _CODEBOOK_H_
#define _CODEBOOK_H_

#include "cv.h"
#include "highgui.h"

#define CHANNELS 3

class Element{
    public:
        uchar learnHigh[CHANNELS];
        uchar learnLow[CHANNELS];
        uchar max[CHANNELS];
        uchar min[CHANNELS];
        int t_last_update;
        int stale;

        bool match(uchar *p, int nChannels = 3);
        void update(uchar *p, int count_access, int nChannels = 3);
};

class PixelElements{
    public:
        Element *elements;
        int num_entries;
        int count_access;

        PixelElements();

        int updatePixel(uchar *p, unsigned *bounds, int nChannels = 3);
        void addElement(Element *new_element);
        uchar backgroundDetect(uchar *p, int *minMod, int *maxMod, int nChannels = 3);
        int clearStaleEntries();
};

class Codebook{
    private:
        IplImage *yuvImage;
        int image_len;
        PixelElements *pixels;
        int maxMod[CHANNELS], minMod[CHANNELS];
        unsigned bounds[CHANNELS];
        bool ch[CHANNELS];
        int nChannels;

    public:
        Codebook();
        void init(IplImage *image);
        void find_foreground(IplImage *image, IplImage *foreground);
};

#endif
